package com.app.view 
{
	import com.peachcastle.debug.Log;
	
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	import flash.display.MovieClip;
	import flash.system.Security;
	import flash.display.Sprite;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import flash.ui.ContextMenuBuiltInItems;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import com.app.view.manager.DialogManagerMediator;
	
	import com.app.view.loading.HallLoading;
	import com.app.view.loading.RoomLoading;
	import com.app.view.loading.ConnectLoading;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class AppMediator extends Mediator implements IMediator
	{
		public static const NAME:String 					= "AppMediator";
		public static const HALL_LOAD_PROGRESS:String 		= NAME + '_HallLoadProgress';
		public static const HALL_LOAD_OK:String 			= NAME + '_HallLoadOk';
		public static const HALL_CONNECT_PROGRESS:String 	= NAME + '_HallConnectProgress';
		public static const HALL_CONNECT_OK:String 			= NAME + '_HallConnectOk';
		public static const ROOM_LOAD_PROGRESS:String 		= NAME + '_RoomLoadProgress';
		public static const ROOM_LOAD_OK:String 			= NAME + '_RoomLoadOk';
		public static const ROOM_CONNECT_PROGRESS:String 	= NAME + '_RoomConnectProgress';
		public static const ROOM_CONNECT_OK:String 			= NAME + '_RoomConnectOk';
		
		public static const NOTICE_RESPONSE:String 			= NAME + '_NoticeResponse';
		
		public static const FEEDBACK_SEND_INFO_REQUEST:String 	= NAME + '_FeedbackSendInfoRequest';
		public static const FEEDBACK_SEND_INFO_RESPONSE:String 	= NAME + '_FeedbackSendInfoResponse';
		
		public static const FEEDBACK_SEND_AFFIX_REQUEST:String 	= NAME + '_FeedbackSendAffixRequest';
		public static const FEEDBACK_SEND_AFFIX_RESPONSE:String 	= NAME + '_FeedbackSendAffixResponse';

		private var _hallLoadUi:HallLoading = null;
		private var _roomLoadUi:RoomLoading = null;
		private var _connectUi:ConnectLoading = null;
		
		private static const VERSION:String = '0.01';
		
		public function AppMediator(viewComponent:Object) 
		{
			super(NAME, viewComponent);
			
			Security.allowDomain("*");
			
			// Stage
			main.stage.align = StageAlign.TOP_LEFT;
			main.stage.scaleMode = StageScaleMode.NO_SCALE;
		}
		
		override public function onRegister():void
		{
			if (ApplicationDomain.currentDomain.hasDefinition("HallLoad"))
			{
				var hloadui:MovieClip = new (ApplicationDomain.currentDomain.getDefinition("HallLoad"));
				main.addChild(hloadui);
				
				_hallLoadUi = new HallLoading(hloadui);
				hloadui = null;
			}

			if (ApplicationDomain.currentDomain.hasDefinition("RoomLoad"))
			{
				var rloadui:MovieClip = new (ApplicationDomain.currentDomain.getDefinition("RoomLoad"));
				main.addChild(rloadui);
				
				_roomLoadUi = new RoomLoading(rloadui);
				rloadui = null;
			}
			
			if (ApplicationDomain.currentDomain.hasDefinition("Connect"))
			{
				var cloadui:MovieClip = new (ApplicationDomain.currentDomain.getDefinition("Connect"));
				main.addChild(cloadui);
				
				_connectUi = new ConnectLoading(cloadui);
				cloadui = null;
			}
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				AppMediator.HALL_LOAD_PROGRESS,
				AppMediator.HALL_LOAD_OK,
				AppMediator.HALL_CONNECT_PROGRESS,
				AppMediator.HALL_CONNECT_OK,
				AppMediator.ROOM_LOAD_PROGRESS,
				AppMediator.ROOM_LOAD_OK,
				AppMediator.ROOM_CONNECT_PROGRESS,
				AppMediator.ROOM_CONNECT_OK,
				AppMediator.NOTICE_RESPONSE
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var param:* = notification.getBody();

			if (Log.console && name != AppMediator.NOTICE_RESPONSE) return;
			
			switch ( name )
			{
				case AppMediator.HALL_LOAD_PROGRESS:
					_hallLoadUi.show(param[0]);
				
					break;
					
				case AppMediator.HALL_CONNECT_PROGRESS:
				case AppMediator.ROOM_CONNECT_PROGRESS:
					_connectUi.showSocket(param[0]);
				
					break;
					
				case AppMediator.HALL_LOAD_OK:
					_hallLoadUi.hide();
				
					break;
				
				case AppMediator.HALL_CONNECT_OK:
				case AppMediator.ROOM_CONNECT_OK:
					_connectUi.hide();
				
					break;
					
				case AppMediator.ROOM_LOAD_PROGRESS:
					_roomLoadUi.showPercent(param[0]);
					
					break;
	
				case AppMediator.ROOM_LOAD_OK:
					_roomLoadUi.hide();
					
					break;
					
				case AppMediator.NOTICE_RESPONSE://提示
					sendNotification(DialogManagerMediator.SHOW_DIALOG, ['systemTipView', param[0]]);

					break;
			}
		}
		
		protected function get main():Main
		{
			return viewComponent as Main;
		}
		
	}

}